﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 文华财经数据解析.utils.WHDataServer
{
    internal class PacketHelper
    {
        static Dictionary<string, byte> filepathDic = new Dictionary<string, byte>() 
        {
            { "GFEX", 0x14},
            { "DCE", 0x03},
            { "CZCE", 0x02},
            { "CFFEX", 0x2f},
            { "INE", 0x1e},
            { "SHFE", 0x04},
            { "SHME", 0x05},
            { "SHSE", 0x00},
            { "亚洲指数",0x06},
            { "CBOE",0x2b},
            { "CBOT",0x07},
            { "COMEX-HM",0x38},
            { "IPE-R",0x3c},
            { "LME场外",0x0b},
            { "NYBOT-R",0x36},
            { "NYME-R",0x3d},
            { "SGX期指",0x2e},
            { "TOCOM",0x16},
            { "外汇_D",0x37},
            { "恒生期指",0x0d},
            { "欧美指数",0x0f},
            { "马来西亚R",0x35},


            //CBOE  0x2b
            //CBOT  0x07
            //COMEX-HM  0x38
            //IPE-R  0x3c
            //LME场外  0x0b
            //NYBOT-R  0x36
            //NYME-R  0x3d
            //SGX期指  0x2e
            //TOCOM  0x16
            //外汇_D  0x37
            //恒生期指  0x0d
            //欧美指数  0x0f
            //马来西亚R 0x35


        };
        public static byte[] KlineReqPacketBuilder(string filepath, string code, DateTime date, ushort reqID) 
        {
            //
            byte[] peer1_3 = { /* Packet 333 */
                0x3a, 0xe6, 0x62, 0x00, 0x02, 0x02, 0x06, 0x00,
                0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd9, 0x01,
                0x00, 0x00, 0x14, 0x64, 0x19, 0xd7, 0x07, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0xd5, 0xf0, 0x37, 
                0x67, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00
            };

          
            {
                ushort mycodeName = ushort.Parse(code.Substring(3));
                peer1_3[42] = filepathDic[filepath];
                var codebt = BitConverter.GetBytes(mycodeName);
                Array.Copy(codebt, 0, peer1_3, 43, 2);
                var time = new DateTimeOffset(date).ToUnixTimeSeconds();
                var timebt = BitConverter.GetBytes(time);
                Array.Copy(timebt, 0, peer1_3, 53, 4);

                var reqidbt = BitConverter.GetBytes(reqID);
                Array.Copy(reqidbt, 0, peer1_3, 38, 2);
                return peer1_3;
            }

           
        }

        public static int GetContentLength(List<byte[]> datas) 
        {
            int res = 0;
            foreach (byte[] data in datas)
            {
                res += data.Length;
            }
            return res;
        }

        //internal static Int16 GetPackLength(List<byte[]> tempData)
        //{
        //    byte[] lenbt = new byte[2];
        //    int i = 0;
        //    foreach (byte[] data in tempData) 
        //    {
        //        foreach (var bt in data)
        //        {
        //            i++;
        //            if (i == 3)
        //            {
        //                lenbt[0] = bt;
        //
        //            }
        //            else if (i == 4) 
        //            {
        //                lenbt[1] = bt;
        //                break;
        //            }
        //        }
        //        if (i==4)
        //        {
        //            break;
        //        }
        //    }
        //    return BitConverter.ToInt16(lenbt, 0);
        //}

        internal static byte[] BuildPack(List<byte[]> sourceData, int packetlen)
        {
            int sourceLen = GetContentLength(sourceData);
            byte[] packet = new byte[packetlen];
            int currindex = 0;
            foreach (byte[] data in sourceData) 
            {
                if (currindex+data.Length<= packetlen)
                {
                    Array.Copy(data, 0, packet, currindex, data.Length);
                    currindex += data.Length;
                }
                else
                {
                    if (currindex < packetlen)
                    {
                        Array.Copy(data, 0, packet, currindex, data.Length - (sourceLen - packetlen));
                    }
                    break;
                }
            }
            return packet;
        }


        public static UInt16 GetPackLength(List<byte[]> datas)
        {
            byte[] startbt = new byte[2];
            int index = 0;
            byte[] lengthbt = new byte[2];
            foreach (byte[] data in datas)
            {
                foreach (var item in data)
                {
                    if (index == 0 || index == 1)
                    {
                        startbt[index] = item;
                    }
                    else
                    {
                        if (startbt[0] == 0x3a && startbt[1] == 0xe6)
                        {
                            if (index == 2 || index == 3)
                            {
                                lengthbt[index - 2] = item;
                                if (index == 3)
                                {
                                    return (ushort)(BitConverter.ToUInt16(lengthbt,0) + 4);
                                }
                            }
                        }
                        else if (startbt[0] == 0x3a && startbt[1] == 0x64)
                        {
                            if (index == 2 || index == 3)
                            {
                                lengthbt[index - 2] = item;
                                if (index == 3)
                                {
                                    return (ushort)(BitConverter.ToUInt16(lengthbt,0) + 4);
                                }
                            }
                        }
                        else if (startbt[0] == 0x3a && startbt[1] == 242)
                        {
                            if (index == 2 || index == 3)
                            {
                                lengthbt[index - 2] = item;
                                if (index == 3)
                                {
                                    return (ushort)(BitConverter.ToUInt16(lengthbt, 0) + 4);
                                }
                            }
                        }
                        else if (startbt[0] == 0x3a && startbt[1] == 176)
                        {
                            if (index == 2 || index == 3)
                            {
                                lengthbt[index - 2] = item;
                                if (index == 3)
                                {
                                    return (ushort)(BitConverter.ToUInt16(lengthbt, 0) + 4);
                                }
                            }
                        }
                        else if (startbt[0] == 0x3a && startbt[1] == 0x6c)
                        {
                            if (index == 6 || index == 7)
                            {
                                lengthbt[index - 6] = item;
                                if (index == 7)
                                {
                                    return (ushort)(BitConverter.ToUInt16(lengthbt,0) + 8);
                                }
                            }
                        }
                        else if (startbt[0] == 0x3a && startbt[1] == 0x56)
                        {
                            return 4;
                        }
                        else
                        {
                            Console.WriteLine("未知头");
                        }
                    }
                    index++;
                    if (index > 7)
                    {
                        break;
                    }
                }
                if (index > 7)
                {
                    break;
                }
            }
            return (ushort)GetContentLength(datas);
        }




    }
}
